using System;
using System.IO;
using System.Runtime.CompilerServices;
using Pinwheel.Toolkits;

namespace Pinwheel.LSerialize.Formatters
{
    // for unamanged types( https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types )
    // * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool
    // * Any enum type
    // * Any pointer type
    // * Any user-defined struct type that contains fields of unmanaged types only
    [Preserve]
    public sealed unsafe class UnmanagedArrayFormatter<T> : LFormatter<T[]>
        where T : unmanaged, IEquatable<T>
    {
        [Preserve]
        public override void Serialize(LSerializeContext* context, LStreamWriter* writer, ref T[] value)
        {
            Profiler.Start("UnmanagedArrayFormatter.Serialize");
            writer->WriteUnmanagedArray<T>(ref value);
            Profiler.End();
        }
    
        [Preserve]
        public override void Deserialize(LSerializeContext* context, LStreamReader* reader, ref T[] value)
        {
            Profiler.Start("UnmanagedArrayFormatter.Deserialize");
            value = reader->ReadUnmanagedArray<T>();
            Profiler.End();
            
        }
    }
    
    [Preserve]
    public sealed unsafe class ArrayFormatter<T> : LFormatter<T[]> where T : IEquatable<T>
    {
        [Preserve]
        public override void Serialize(LSerializeContext* context, LStreamWriter* writer, ref T[] value)
        {
            Profiler.Start("ArrayFormatter.Serialize");
            writer->WriteArray(context, ref value);
            Profiler.End();
        }
    
        [Preserve]
        public override void Deserialize(LSerializeContext* context, LStreamReader* reader, ref T[] value)
        {
            Profiler.Start("ArrayFormatter.Deserialize");
            reader->ReadArray(context, ref value);
            Profiler.End();
        }
    }
}